package com.zll.dms.service.impl;

import com.zll.dms.constants.Constants;
import com.zll.dms.constants.type.BaseRole;
import com.zll.dms.exception.MyBaseException;
import com.zll.dms.mapper.DocMapper;
import com.zll.dms.mapper.UserMapper;
import com.zll.dms.model.Doc;
import com.zll.dms.service.DocService;
import com.zll.dms.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * doc业务实现类
 */
@Slf4j
@Service
public class DocServiceImpl implements DocService {

    private static final Executor saveFileExecutor = Executors.newFixedThreadPool(4);

    @Autowired
    private DocMapper docMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addDoc(Doc doc) {
        doc.setCreateTime(Calendar.getInstance().getTime());

        doc.setUserId(CommonUtils.getCurrentUserId(userMapper));
        int insert = docMapper.insert(doc);
        if (insert <= 0) {
            throw new MyBaseException("新增文档失败");
        }
    }

    @Override
    public List<Doc> getCurrentUserDocList(String keyWord, long limitNum) {
        Long currentUserId = CommonUtils.getCurrentUserId(userMapper);
        List<Doc> currentUserDocList = docMapper.getCurrentUserDocList(currentUserId, keyWord, limitNum);
        return currentUserDocList;
    }

    @Override
    public void update(Doc doc) {
        //判断当前登录人和doc所属人id是否相同
        Long currentUserId = CommonUtils.getCurrentUserId(userMapper);
        Subject subject = SecurityUtils.getSubject();
        if ((!currentUserId.equals(doc.getUserId()))&&(!subject.hasRole(BaseRole.ADMIN))) {
            throw new MyBaseException("你无此文档操作权限");
        }
        int update = docMapper.update(doc);
        if (update <= 0) {
            throw new MyBaseException("修改文档失败");
        }
    }

    @Override
    public void deleteById(Long id) {
        //判断当前登录人和doc所属人id是否相同
        Long currentUserId = CommonUtils.getCurrentUserId(userMapper);
        Doc doc = docMapper.queryById(id);
        Subject subject = SecurityUtils.getSubject();
        if ((!currentUserId.equals(doc.getUserId()))&&(!subject.hasRole(BaseRole.ADMIN))) {
            throw new MyBaseException("你无此文档操作权限");
        }
        int i = docMapper.deleteById(id);
        if (i <= 0) {
            throw new MyBaseException("删除文档失败");
        }
    }

    @Override
    public List<Doc> getAllUserDocList(String keyWord, String userName, long limitNum) {
        List<Doc> allUserDocList = docMapper.getAllUserDocList(keyWord, userName, limitNum);
        return allUserDocList;
    }

    @Override
    public String saveDocFile(String basePath, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension;
        File fileDir = new File(basePath);
        if (!fileDir.exists()) {
            fileDir.mkdir();
        }
        String newPath = basePath  + File.separator + newFileName;
        File newFile = new File(newPath);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            log.error("保存文件失败", e);
            throw new MyBaseException("保存文件失败");
        }
        return Constants.SERVER_BASE_URL + newFileName;
    }


}
